﻿namespace Methods
{
    using System;
    using System.Collections.Generic;

    class SumOfTwoPolynomials
    {   
        // 1.1.9.12. Write a method that calculates the sum of two polynomials with integer coefficients,
        // for example (3x2 + x - 3) + (x - 1) = (3x2 + 2x - 4).

        public static List<int> SumPolynomialsMembers(List<int> sumPoly, int x, int coef)
        {
            if (x > sumPoly.Count)
            {
                int y = sumPoly.Count;
                for (int i = 0; i <= x - y; i++)
                {
                    sumPoly.Add(0);
                }
            }

            sumPoly[x] += coef;

            return sumPoly;
        }

        public static List<int> SumTwoPolinomials(List<int> first, List<int> second)
        {
            int shorter = first.Count;
            int longer = second.Count;
            if (second.Count < first.Count)
            {
                shorter = second.Count;
                longer = first.Count;
            }
            List<int> result = new List<int>(longer);

            

            for (int i = 0; i < shorter; i++)
            {
                result = SumPolynomialsMembers(result, i, first[i]);
                result = SumPolynomialsMembers(result, i, second[i]);
            }

            return result;
        }

        static void Main()
        {
            List<int> firstPoly = new List<int>();
            List<int> secondPoly = new List<int>();

            List<int> sumPoly = new List<int>();
            sumPoly = SumTwoPolinomials(firstPoly, secondPoly);

            // Well, the program is not finished but the task (to write a method) is.
        }
    }
}